package com.nhc.activity_fragment;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.sdk.android.feedback.impl.FeedbackAPI;
import com.astrob.NavFrameSDK;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.astrob.util.Utils;
import com.nhc.model.VCodeFileHandle;
import com.nhc.serviceapi.ServiceApi;
import com.nhc.serviceapi.ServiceRequestCallBack;

import java.lang.reflect.Method;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.EditTextInfoActivity;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.ui.WebViewActivity;
import zuo.biao.library.util.StringUtil;

//http://megowx.com/we7/bhcnav.php?a=vcode&deviceid=6E84466582910000
public class AboutActivity extends BaseActivity implements View.OnClickListener {
    private final String TAG = "AboutActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, AboutActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private TextView tv_version;
    private TextView tv_deviceid;
    private TextView tv_license;
    TextView tv_osversion;

    @Override
    public void initView() {//必须调用
        tv_version = (TextView) findViewById(R.id.tv_version);
        tv_version.setText(Utils.getVersion(this));

        tv_osversion = (TextView) findViewById(R.id.tv_osversion);
        tv_osversion.setText("OS版本：" + Build.DISPLAY);

        tv_deviceid = (TextView) findViewById(R.id.tv_deviceid);
        tv_license = (TextView) findViewById(R.id.tv_license);

        String code = VCodeFileHandle.getInstance().getDeviceID();
        tv_deviceid.setText(getString(R.string.about_deviceid) + code);
        if (code.length() < 1) {
            findViewById(R.id.tv_input).setVisibility(View.GONE);
        }

        if (VCodeFileHandle.getInstance().getRightState() == 1) {
            String licenseKey = VCodeFileHandle.getInstance().getLicenseKey();
            if (licenseKey.length() > 1) {
                tv_license.setText(getString(R.string.about_license) + licenseKey);
            } else {
                tv_license.setText(R.string.about_lincenseok);
            }
            findViewById(R.id.tv_input).setVisibility(View.GONE);
            findViewById(R.id.bt_regetvcode).setVisibility(View.GONE);
        } else {
            tv_license.setText(R.string.about_lincenseno);
        }

        if (_isTest) {
            findViewById(R.id.tv_company).setVisibility(View.GONE);
        }
    }

    boolean _isTest = false;

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initData() {//必须调用

    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用

    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void onForwardClick(View v) {
        intent = EditTextInfoActivity.createIntent(context, EditTextInfoActivity.TYPE_NICK
                , getString(R.string.about_inputtitle), "");
        toActivity(intent, REQUEST_TO_EDIT_TEXT_INFO, false);
    }

    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    private void checkVersion(String licensekey) {

        String deviceid = VCodeFileHandle.getInstance().getDeviceID();
        if (deviceid.length() < 1) {
            showShortToast(getString(R.string.nodeviceid));
            return;
        }
        VCodeFileHandle.getInstance().setLicenseKey(licensekey);
        int rightState = VCodeFileHandle.getInstance().reloadRightState();

        if (rightState != 1) {
            showShortToast(getString(R.string.about_inputerro));
            return;
        }

        showShortToast(getString(R.string.about_inputok));
        tv_license.setText(getString(R.string.about_license) + licensekey);
        findViewById(R.id.tv_input).setVisibility(View.GONE);
        findViewById(R.id.bt_regetvcode).setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {

    }

    private ProgressDialog mPB;

    public void onGetVcode(View v) {
        if (!Utils.isNetConnected(getBaseContext())) {
            showShortToast("请先连接网络！");
            return;
        }

        String deviceid = VCodeFileHandle.getInstance().getDeviceID();
        if (deviceid.length() < 1) {
            showShortToast(getString(R.string.nodeviceid));
            return;
        }

        mPB = ProgressDialog.show(this, "正在激活", "激活中...");
        ServiceApi.regetvcode(deviceid, new ServiceRequestCallBack<ServiceApi.VcodeInfo>() {
            @Override
            public void onSuccess(ServiceApi.VcodeInfo result) {
                if (mPB != null) {
                    mPB.cancel();
                    mPB = null;
                }
                checkVersion(result.vcode);
            }

            @Override
            public void onFailure(int errCode, String msg) {
                if (mPB != null) {
                    mPB.cancel();
                    mPB = null;
                }
                Toast.makeText(AboutActivity.this, msg, Toast.LENGTH_LONG).show();
            }
        });

    }

    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    private final int REQUEST_TO_EDIT_TEXT_INFO = 23;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_EDIT_TEXT_INFO:
                if (data != null) {
                    String name = StringUtil.getTrimedString(
                            data.getStringExtra(EditTextInfoWindow.RESULT_VALUE));
                    checkVersion(name);
                }
                break;
        }
    }

    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
